package com.excuse.excusebuilder.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.view.activity.CommunityItemDetailActivity;
import com.excuse.excusebuilder.viewholder.CommunityFragmentViewHolder;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by U-nookia on 2017/4/18.
 */

public class CommunityFragmentAdapter extends RecyclerView.Adapter<CommunityFragmentViewHolder>{

    private List<Post> posts;
    private Context context;

    public CommunityFragmentAdapter(List<Post> posts) {
        this.posts = posts;
    }

    @Override
    public CommunityFragmentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new CommunityFragmentViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_community,parent,false));
    }

    @Override
    public void onBindViewHolder(CommunityFragmentViewHolder holder, final int position) {
        RelativeLayout item = holder.getView(R.id.community_item);
        CircleImageView head = holder.getView(R.id.item_community_head);
        TextView name = holder.getView(R.id.item_community_name);
        TextView title = holder.getView(R.id.item_community_title);
        final TextView content = holder.getView(R.id.item_community_content);
        TextView time = holder.getView(R.id.item_community_time);
        Post post = posts.get(position);
        Glide.with(context).load(post.getUser().getAvatarUrl()).into(head);
        name.setText(post.getUser().getUsername());
        title.setText(post.getTitle());
        content.setText(post.getContent());
        time.setText(post.getCreatedAt());
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(context,CommunityItemDetailActivity.class);
                bundle.putSerializable("item",posts.get(position));
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }
}
